package com.cgsd.heyu.ui.task.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import com.android.library.image.GlideUtil;
import com.android.library.utils.DpPxUtil;
import com.android.library.utils.LogUtils;
import com.cgsd.heyu.R;
import com.cgsd.heyu.common.AvatarManger;
import com.cgsd.heyu.ui.task.vo.DoTaskDetailVo;
import com.cgsd.heyu.ui.task.vo.TempCommitBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: sky on 2021/3/23 16:17
 * Email: xcode126@126.com
 * Desc: 完成任务进度-step
 */
public class DoStepAdapter extends BaseQuickAdapter<DoTaskDetailVo.InfoBean.StepBean, BaseViewHolder> {

    public DoStepAdapter() {
        super(R.layout.item_step_do);
    }

    @Override
    protected void convert(BaseViewHolder helper, DoTaskDetailVo.InfoBean.StepBean item) {
        handleState(helper, item);
        helper.setText(R.id.tv_step, "" + item.step);
        helper.setText(R.id.tv_step_title, "步骤" + item.step);
        helper.setText(R.id.tv_title, TextUtils.isEmpty(item.title) ? "暂无描述标题" : item.title);
        AvatarManger.setAvatar(mContext, item.image, helper.getView(R.id.iv_example));
        if (TextUtils.isEmpty(item.imagePath)) {
            helper.setImageResource(R.id.iv_picture, R.mipmap.ic_add_picture);
            helper.setGone(R.id.iv_close, false);
        } else {
            GlideUtil.loadCircularAllImage(mContext, item.imagePath, helper.getView(R.id.iv_picture), DpPxUtil.dip2px(mContext, 8));
            helper.setGone(R.id.iv_close, true);
        }
        helper.addOnClickListener(R.id.iv_close);
        helper.addOnClickListener(R.id.iv_picture);
        helper.addOnClickListener(R.id.rl_example);
        handleInput(helper, item);
    }

    /**
     * 处理图片和文本的状态
     *
     * @param helper
     * @param item
     */
    private void handleState(BaseViewHolder helper, DoTaskDetailVo.InfoBean.StepBean item) {
        if (TextUtils.equals(item.backcom, "0") && TextUtils.equals(item.backimg, "0")) {//图片和文本都不显示
            helper.setGone(R.id.et_content, false);
            helper.setGone(R.id.fl_picture, false);
            helper.setGone(R.id.rl_example, true);//示例图片要显示
        } else {
            if (TextUtils.equals(item.backimg, "1")) {//图片显示，文本隐藏
                helper.setGone(R.id.rl_example, true);
                helper.setGone(R.id.fl_picture, true);
                helper.setGone(R.id.et_content, false);
            } else {//图片隐藏，文本显示
                helper.setText(R.id.et_content, item.inputText);
                helper.setGone(R.id.et_content, true);

                helper.setGone(R.id.rl_example, false);
                helper.setGone(R.id.fl_picture, false);
            }
        }
    }

    /**
     * get param
     *
     * @return
     */
    public String getParam() {
        List<TempCommitBean> list = new ArrayList<>();
        if (mData != null && mData.size() > 0) {
            for (DoTaskDetailVo.InfoBean.StepBean stepBean : mData) {
                //类型为图片，切图片路径为空，则提示
                if (TextUtils.equals(stepBean.backimg, "1") && TextUtils.isEmpty(stepBean.imagePath)) {
                    return "";
                }

                //类型为文字，切文本为空，则提示
                if (TextUtils.equals(stepBean.backcom, "1") && TextUtils.isEmpty(stepBean.inputText)) {
                    return "";
                }
                list.add(new TempCommitBean(stepBean.step, stepBean.inputText, stepBean.imagePath));
            }
            if (list.size() > 0) {//该判断保证提交内容不能为空
                //保持值为null的字段不会抹除
                return new GsonBuilder().serializeNulls().create().toJson(list);
            } else {
                return "";
            }
        } else {
            return "";
        }
    }

    /**
     * 处理输入框
     *
     * @param helper
     * @param item
     */
    private void handleInput(BaseViewHolder helper, DoTaskDetailVo.InfoBean.StepBean item) {
        EditText etContent = helper.getView(R.id.et_content);
        final TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                LogUtils.e("textWatcher", helper.getAdapterPosition() + "");
                if (etContent.hasFocus()) {//判断当前EditText是否有焦点在
                    String trim = etContent.getText().toString().trim();
                    item.inputText = trim;
                }
            }
        };
        //设置EditText的焦点监听器判断焦点变化，当有焦点时addTextChangedListener，失去焦点时removeTextChangedListener
        etContent.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                etContent.addTextChangedListener(textWatcher);
                LogUtils.e("addTextChanged", helper.getAdapterPosition() + "");
            } else {
                etContent.removeTextChangedListener(textWatcher);
                LogUtils.e("removeTextChanged", helper.getAdapterPosition() + "");
            }
        });
    }

}
